﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Quản_Lý_Thư_Viện
{
    public partial class frmTraTien : Form
    {
        public frmTraTien()
        {
            InitializeComponent();
            frmQuyDinh.laygiatriquydinh();
        }

        DocgiaControl dgcontrol = new DocgiaControl();
        DataTable dt = new DataTable();
        public static string mdgmp;
        public static int stn;

        private void frmTratien_Load(object sender, EventArgs e)
        {
            
            if (frmChinh.check == true)
            {
                txtMaDG.Text = frmPhieuTraSach.madg1;
                dt = dgcontrol.Laysotienno();
                DataRow dr = dt.Rows[0];
                txtSoTienNo.Text = dr["SOTIENNO"].ToString();
                txtTriGiaPTS.Text = frmPhieuTraSach.trigiapts.ToString();
                btnN.Enabled = false;
            }
            else
            {
                //mdgmp = txtMaDG.Text;
                //dt = dgcontrol.Laystnptt();
                //DataRow dr = dt.Rows[0];
                //txtSoTienNo.Text = dr["SOTIENNO"].ToString();
                txtTriGiaPTS.Enabled = false;
                txtTriGiaPTS.Text = "0";
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (txtMaDG.Text == "" || txtSoTienDua.Text == "")
            {
                MessageBox.Show("Vui lòng nhập đầy đủ thông tin");

            }
            else
            {
                if (Convert.ToInt32(txtSoTienThoi.Text) > 0)
                {
                    stn = 0;
                    dgcontrol.updatestn();
                }
                else
                {
                    
                    stn = Convert.ToInt16(txtSoTienNo.Text)+Convert.ToInt16(txtTriGiaPTS.Text) - Convert.ToInt16(txtSoTienDua.Text);
                    dgcontrol.updatestn();
                }
                MessageBox.Show("Đã thanh toán!");
            }
            if (Convert.ToInt32(txtSoTienNo.Text) > frmQuyDinh.tiennotoida)
            {
                dgcontrol.updatetrangthai();
            }
            else
            {
                dgcontrol.updatetrangthaikvp();
            }
            this.Close();
        }       
         private void KiemTraNhapSo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Yêu Cầu Nhập Số");
            }
        }
         protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
         {
             if (keyData == (Keys.Escape))
             {
                 this.Close();
             }
             return base.ProcessCmdKey(ref msg, keyData);
         }

         private void txtSoTienDua_TextChanged(object sender, EventArgs e)
         {
             if (txtMaDG.Text == "")
             {
                 MessageBox.Show("Vui lòng nhập mã độc giả");

             }
             else
             {
                 if ((Convert.ToInt64(txtSoTienDua.Text) - Convert.ToInt64(txtSoTienNo.Text)) < 0)
                 {
                     txtSoTienThoi.Text = "0";
                 }
                 else
                 {
                     if (frmChinh.check == true)
                     {
                         txtSoTienThoi.Text = (Convert.ToInt64(txtSoTienDua.Text) - Convert.ToInt64(txtSoTienNo.Text) - Convert.ToInt64(txtTriGiaPTS.Text)).ToString();
                     }
                     else
                         txtSoTienThoi.Text = (Convert.ToInt64(txtSoTienDua.Text) - Convert.ToInt64(txtSoTienNo.Text)).ToString();
                 }
                 //txtSoTienNo.Text = (Convert.ToInt64(txtSoTienNo.Text) - Convert.ToInt64(txtSoTienDua.Text)).ToString();
             }
         }

         private void btnN_Click(object sender, EventArgs e)
         {
             mdgmp = txtMaDG.Text;
             dt = dgcontrol.Laystnptt();
             DataRow dr = dt.Rows[0];
             txtSoTienNo.Text = dr["SOTIENNO"].ToString();
         }


       
    }
}
